﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using RBOSS.Business.Presenters;
using RBOSS.Business.Products;
using RBOSS.Business.Views;
using RBOSS.WebUI.Models;

namespace RBOSS.WebUI
{
    public partial class ProductDetailsForm : MasterSite, IProductView
    {
        public IProduct Product { get; set; }
        private readonly ProductPresenter _productPresenter;

        public ProductDetailsForm()
        {
            _productPresenter = new ProductPresenter(this);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ProductId = 0;
            if (Request.QueryString["Id"] != null)
            {
                ProductId = int.Parse(Request.QueryString["Id"]);
            }
            if (!Page.IsPostBack)
            {
                BindQunatity();
            }

            Product = _productPresenter.GetProduct(ProductId);
            WireupEvent();
        }

        private void BindQunatity()
        {
            for (int i = 1; i < 10; i++)
            {
                ddlQty.Items.Add(new ListItem(i.ToString(),i.ToString()));
            }
        }

        private void WireupEvent()
        {
            butAddToCart.Click += AddToCartClick;
        }

        private void AddToCartClick(object sender, EventArgs e)
        {
            _productPresenter.AddItemToCart(ProductId,int.Parse(ddlQty.SelectedValue));
            this.ClientScript.RegisterStartupScript(this.GetType(), "ItemAdded", "alert('Item has been added successfully')", true);
        }

        public int ProductId
        {
            get;
            set;
        }
    }
}